package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.DynamicDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.thread.LoopFileThread;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.MD5;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class Login extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int sid = TextUtil.String2int(request.getParameter("schoolid"), -1);
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		pwd = MD5.getMD5(MD5.getMD5(name + MD5.getMD5(pwd)));// 数据库的md5
		int juese = TextUtil.String2int(request.getParameter("juese"),Constant.ULEVEL_forbid);
		String device = request.getHeader("user-agent");

		Connection conn = DBUtil.getConnection();
		if (conn == null) {
			Repost.dberror(request, response);
			return;
		}
		
		if (sid==-1) {
			Repost.message("请选择学校!", request, response);
			return;
		}
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		UserDao udao = dao.getUserDao();
		if (!udao.isRegister(sid, name)) {
			Repost.message("账号错误", request, response);
			return;
		}

		UserInfo info = udao.login(sid, name, pwd, "web", device,Constant.LOGINTYPE_WEB);
		if (info == null) {
			Repost.message("密码错误!", request, response);
			return;
		}
		
		if (info.getUlevel() == Constant.ULEVEL_forbid) {
			Repost.message("该账号已被禁用，请联系管理员!", request, response);
			return;
		}
		if (info.getUlevel() < juese) {
			Repost.message("你还没有权限登陆哦!", request, response);
			return;
		}

		String LoginCookie = info.getLogincookie();
		Cookie cookie = new Cookie("LoginCookie", LoginCookie);
		cookie.setMaxAge(60 * 60 * 12);// 一天超时
		response.addCookie(cookie);
		//test
		Cookie cookie2 = new Cookie("LoginCookie_test", "value");
		cookie2.setMaxAge(60 * 60 * 30);// 30天超时
		response.addCookie(cookie2);
		
		if (info.getUlevel()==Constant.ULEVEL_superadmin) {//超级管理员//隐藏进入
			Repost.success("menu_super.jsp", request, response);
		} else if (juese == Constant.ULEVEL_user) {
			Repost.success("menu_user.jsp", request, response);
		} else if (juese == Constant.ULEVEL_teacher) {
			Repost.success("menu_teacher.jsp", request, response);
		} else if (juese == Constant.ULEVEL_admin) {
			Repost.success("menu_admin.jsp", request, response);
		} else {
			Repost.message("角色选择错误!", request, response);
		}
		
		
		
	}
}
